package logic

import (
	"context"
	"errors"
	"video/models/mysql"

	"video/videorpc/internal/svc"
	"video/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type VideoDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoDelLogic {
	return &VideoDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 下架视频
func (l *VideoDelLogic) VideoDel(in *videorpc.VideoDelRequest) (*videorpc.VideoDelResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.FirstUserByUserId(mysql.DB, in.UserId)
	if err != nil {
		return nil, err
	}
	if user.ID == 0 {
		return nil, errors.New("用户未注册,不允许下架视频")
	}

	var video mysql.Video
	if in.VideoStatus == 0 {
		videoUpdate := &mysql.Video{
			Model:         gorm.Model{ID: video.ID},
			VideoTitle:    in.VideoTitle,
			VideoCategory: in.VideoCategory,
			VideoLink:     in.VideoLink,
		}
		err = videoUpdate.UpdateVideo(mysql.DB, in.VideoId)
		if err != nil {
			return nil, errors.New("修改失败")
		}
	}
	if in.VideoStatus == 2 {
		err = video.VideoDel(mysql.DB, in.VideoId, in.UserId)
		if err != nil {
			return nil, errors.New("下架失败")
		}
	}

	return &videorpc.VideoDelResponse{}, nil
}
